package com.cnpinxi.controller.admin.activity;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.Activity;
import com.cnpinxi.domain.ProductEntity;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ActivityService;
import com.cnpinxi.service.ProductEntityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/admin/activity")
@Controller
public class AdminActivityController extends BaseController {
    @Autowired
    ActivityService activityService;
    @Autowired
    ProductEntityService productEntityService;

    /**
     * 查询所有活动
     * @param request
     * @return
     */
    @RequestMapping("/getActivityList")
    public ModelAndView getActivityList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/activity/getActivityList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Activity> activityList = activityService.getActivityList(map);
        PageInfo<Activity> pageinfo = new PageInfo<>(activityList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }


    /**
     * 跳转到产品名称编辑或添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/activity")
    public ModelAndView activity(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/activity/activity");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtils.isNotBlank(tid)){
            Activity activity = activityService.getActivityById(Long.parseLong(tid));
            m.addObject("activity",activity);
        }
        return m;
    }

    /**
     * 编辑活动
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifyActivity")
    @ResponseBody
    public String modifyActivity(HttpServletRequest request){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        Map<String, String> map = this.showRequestParameters(request);
        try {
            activityService.modifyActivity(map);
            resultObj.setCode(ResultObj.CODE_SUCCESS);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 关联活动产品
     * @param request
     * @return
     */
    @RequestMapping(value = "/setActivityProduct")
    @ResponseBody
    public String setActivityProduct(HttpServletRequest request){
        Map<String,String> map =this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        try {
            int num = activityService.insertActivityProductEntityRelation(map);
            //resultObj.setData(productEntitys);
            if(num==1){
                resultObj.setCode(ResultObj.CODE_SUCCESS);
            }
        } catch (MyRunTimeExcption e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
            resultObj.setMsg("系统错误");
        }
        return  resultObj.toJson();
    }

    /**
     * 生成活动二维码
     * @param request
     * @return
     */
    @RequestMapping(value = "/createActivityQRCode")
    @ResponseBody
    public String createActivityQRCode(HttpServletRequest request){
        Map<String,String> map =this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_FAIL);
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        try {
            int num = activityService.createActivityQRCode(map,basePath);
            //resultObj.setData(productEntitys);
            if(num==1){
                resultObj.setCode(ResultObj.CODE_SUCCESS);
            }
        } catch (MyRunTimeExcption e) {
            logger.warn(e.getMessage());
            resultObj.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
            resultObj.setMsg("系统错误");
        }
        return  resultObj.toJson();
    }
    /**
     * 查询活动关联产品
     * @param request
     * @return
     */
    @RequestMapping("/getProductEntityByActivityId")
    @ResponseBody
    public String getProductEntityByActivityId(HttpServletRequest request,Long activityId) {
        Map<String, String> map = this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            List<ProductEntity> ProductEntityList = productEntityService.getProductEntityByActivityId(activityId);
            resultObj.setData(ProductEntityList);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
